<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\admin\controller;

use think\Controller;
use think\Db;

//脚本文件
class Script extends Controller
{   
    //归档资料
    public function inboundDoc()
    {
        $doc_list = db('case_doc_list')->alias('a')
        ->join('case_doc d','d.id = a.case_doc_id')
        ->where(['a.proj_doc_id'=>0,'a.source'=>2,'a.status'=>1,'d.status'=>1])
        ->field('a.id,a.proj_id,a.build_id,b.doc_id,a.doc_url,a.create_uid,a.create_time,a.status,a.file_type')
        ->select();
        if(!empty($doc_list))
        {
            foreach($doc_list as $doc)
            {   
                $case_doc_id = $doc['id'];
                unset($doc['id']);
                $proj_doc_id = Db::table('proj_doc')->insertGetId($doc);
                $res = Db::table('case_doc_list')->where('id',$case_doc_id)->update(['proj_doc_id'=>$proj_doc_id]);
                if ($proj_doc_id && $res)
                {
                    return '操作成功！';
                } else {
                    return '操作失败！';
                }
            }    
        }
        return '操作成功！';
    }

    //查找超时案件
    public function findOvertimeCase()
    {   
        ini_set('max_execution_time', '0');
        Db::execute('truncate table overtime_case_list');
        $start_time = strtotime(date('Y-m-d')) - 86400*1;
        $endDate = time();
        $case_data = [];
        //查询未提交案子
        $case_list = db('case_list')->alias('a')
                ->join('user u','a.create_uid=u.id')
                ->where(['a.wf_type'=>0,'status'=>1,'a.create_time'=>['lt',$start_time]])
                ->field('a.*,u.dept_id')
                ->select();
        
        if ($case_list)
        {   
            foreach($case_list as $case)
            {
                $work_day = getWorkDays($case['create_time'], $endDate);
                $wf_info = db('workflow_conf')->alias('a')
                        ->join('workflow_type t','t.id=a.type_id')
                        ->where(['t.ser_id'=>$case['ser_id'], 'a.step'=>1,'a.isvalid'=>1])
                        ->field('a.*')
                        ->find();

                if ($work_day >= $wf_info['work_day'] && $wf_info['work_day'] > 0) {
                    $data = [];
                    $data['case_id'] = $case['id'];
                    $data['handle_uid'] = $case['create_uid'];
                    $data['handle_dept_id'] = $case['dept_id'];
                    $data['stop_day'] = $work_day;
                    $case_data[] = $data;
                }
                
            }    
        }  
        
        //查询在办理案件
        $case_list = db('case_list')->alias('a')
                ->join('user u','a.create_uid=u.id')
                ->join('workflow_conf f','f.id = a.handle_conf_id')
                ->where(['a.status'=>1,'a.create_time'=>['lt',$start_time],'wf_type'=>['gt',0]])
                ->field('a.*,u.dept_id,f.work_day')
                ->select();

        if ($case_list) {
            foreach($case_list as $case)
            {
                $step_time = db('workflow_step')->where(['case_id'=>$case['id'], 'isvalid'=>1, 'node_id'=>$case['handle_node']])->value('accept_time');
                $work_day = getWorkDays($step_time, $endDate);
                 
                if ($work_day >= $case['work_day'] && $case['work_day'] > 0) {
                    $uids = [];
                    
                    if ($case['handle_type'] == 1) {
                        $uids = db('workflow_group_user')->where(['group_id'=>$case['handle_uid'], 'isvalid'=>1])->column('uid');
                    } else {
                        $uids[] = $case['handle_uid'];
                    }
                    
                    if ($uids) {
                        foreach($uids as $uid)
                        {
                            $data = [];
                            $data['case_id'] = $case['id'];
                            $data['handle_uid'] = $uid;
                            $data['handle_dept_id'] = $case['dept_id'];
                            $data['stop_day'] = $work_day;
                            $case_data[] = $data;
                        }
                    }
                }
            }
        }
        
        //插入数据
        if ($case_data)
        {
            db('overtime_case_list')->insertAll($case_data);
        }
        header("Content-type: text/html; charset=utf-8");
        echo "操作成功！";
    }
    
    
}
